package com.ahm.config;

import com.ahm.base.ResultInfo;
import com.ahm.model.UserModel;
import com.ahm.utils.UserIDBase64;
import com.alibaba.fastjson.JSON;
import org.springframework.security.core.Authentication;

import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/*功能描述
 * @author ahm
 */
public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
//    @Override
//    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest,Authentication authentication ,HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
//
//        ResultInfo resultInfo = new ResultInfo();
//        resultInfo.setCode(200);
//        resultInfo.setMsg("登录成功");
//        //实例化对象
//        UserModel userModel=new UserModel();
//        //加密
//        userModel.setUserIdStr(UserIDBase64.encoderUserID(1));
//        userModel.setUserName("admin");
//        userModel.setTrueName("admin");
//        resultInfo.setResult(userModel);
//        String string = JSON.toJSONString(resultInfo);
//        //返回目标对象
//        httpServletResponse.setContentType("application/json");
//        httpServletResponse.setCharacterEncoding("utf-8");
//        PrintWriter writer = httpServletResponse.getWriter();
//        writer.write(string);
//        writer.close();
//    }

    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        ResultInfo resultInfo = new ResultInfo();
        resultInfo.setCode(200);
        resultInfo.setMsg("登录成功");
        //实例化对象
        String string = JSON.toJSONString(resultInfo);
        //返回目标对象
        httpServletResponse.setContentType("application/json");
        httpServletResponse.setCharacterEncoding("utf-8");
        PrintWriter writer = httpServletResponse.getWriter();
        writer.write(string);
        writer.close();

    }
}
